package com.tanhua.server.serivce;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private SettingsApi settingsApi;
    @Reference
    private QuestionApi questionApi;
    @Reference
    private BlackListApi blackListApi;

    /**
     * 接口名称：用户通用设置 - 读取
     */
    public ResponseEntity<Object> querySettings() {
        // 1. 获取当前认证后的用户
        User user = UserHolder.get();

        // 2. 根据用户id查询通知设置表 tb_settings
        Settings settings = settingsApi.findByUserId(user.getId());
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(user.getId());
            settingsApi.save(settings);
        }

        // 3. 根据用户id查询陌生人问题
        Question question = questionApi.findByUserId(user.getId());

        // 4. 创建vo，封装返回结果
        SettingsVo settingsVo = new SettingsVo();
        // 来自于： db
        BeanUtils.copyProperties(settings, settingsVo);
        settingsVo.setPhone(user.getMobile());
        // 设置陌生人问题
        if (question == null) {
            settingsVo.setStrangerQuestion("你喜欢我吗？");
        } else {
            settingsVo.setStrangerQuestion(question.getTxt());
        }
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 接口名称：通知设置 - 保存
     */
    public ResponseEntity<Object> saveNotifications(Settings param) {
        //1. 获取认证后的用户
        Long userId = UserHolder.getUserId();

        //2. 先根据用户id查询通知设置表
        Settings settings = settingsApi.findByUserId(userId);
        if (settings == null) {
            // 没有找到数据，执行添加
            settings = new Settings();
            // 对象拷贝
            BeanUtils.copyProperties(param, settings);
            // 设置用户id
            settings.setUserId(userId);
            // 执行添加
            settingsApi.save(settings);
        } else {
            // 改用户的通知设置已经存在，执行修改
            settings.setPinglunNotification(param.getPinglunNotification());
            settings.setLikeNotification(param.getLikeNotification());
            settings.setGonggaoNotification(param.getGonggaoNotification());
            // 执行修改
            settingsApi.update(settings);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 设置陌生人问题
     *
     * @param content 页面传入的陌生人问题
     * @return
     */
    public ResponseEntity<Object> saveQuestion(String content) {
        //1. 获取认证后的用户
        Long userId = UserHolder.getUserId();

        //2. 根据用户id查询陌生人问题
        Question question = questionApi.findByUserId(userId);

        //3. 判断
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            // 执行保存
            questionApi.save(question);
        } else {
            question.setTxt(content);
            // 执行修改
            questionApi.update(question);
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 接口名称：黑名单 - 翻页列表
     */
    public ResponseEntity<Object> blacklist(Integer page, Integer pagesize) {
        //1. 获取认证后的用户
        User user = UserHolder.get();

        //2. 根据用户id分页查询黑名单用户
        IPage<UserInfo> iPage = blackListApi.blacklist(page, pagesize, user.getId());

        //3. 封装返回结果
        PageResult pageResult = new PageResult(
                page, pagesize, (int) iPage.getTotal(), iPage.getRecords()
        );
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 接口名称：黑名单 - 移除
     */
    public ResponseEntity<Object> deleteBlackUser(Long blackUserId) {
        // 获取当前登陆用户id
        Long userId = UserHolder.getUserId();
        // 调用dubbo服务，从黑名单列表中移除黑名单用户
        blackListApi.deleteBlackUser(userId, blackUserId);
        return ResponseEntity.ok(null);
    }
}
